using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using DG.Tweening;

public class GameManagerLow : MonoBehaviour
{
    public Transform cupTrans;
    public GameObject cup, gameOverPanel;
    public CreateBall createBall;
    public GameManagerTop managerTop;
    public DestroyBall destroyBall;

    void Start()
    {
        Time.timeScale = 1;
        Tweener tweener = cup.transform.DOLocalRotate(new Vector3(0, 0, -100), 1f);
        createBall.enabled = true;
        createBall.startNum = managerTop.temple;
        
    }

    void Update()
    {
        destroyBall.timer += Time.deltaTime;
        if (Input.GetMouseButton(0) && !EventSystem.current.IsPointerOverGameObject())
        {
            Vector3 screenPos = Camera.main.WorldToScreenPoint(cupTrans.transform.position);
            Vector3 mousePos = Input.mousePosition;
            mousePos.z = screenPos.z;
            mousePos.y = screenPos.y;
            Vector3 worldPos = Camera.main.ScreenToWorldPoint(mousePos);
            if (worldPos.x > 2)
            {
                worldPos.x = 2;
            }
            if (worldPos.x < -2.5)
            {
                worldPos.x = -2.5f;
            }
            cupTrans.position = worldPos;
        }

        if (destroyBall.timer >= 5f)
        {
            
            gameOverPanel.SetActive(true);
            
        }
    }
    public void InitScore()
    {
        PlayerPrefs.SetInt(Const.Score, destroyBall.getNumber);
        int totalScore = PlayerPrefs.GetInt(Const.TotalScore) + destroyBall.getNumber;
        PlayerPrefs.SetInt(Const.TotalScore, totalScore);
        if (PlayerPrefs.GetInt(Const.BestScore) < destroyBall.getNumber)
        {
            PlayerPrefs.SetInt(Const.BestScore, destroyBall.getNumber);
        }
        if (destroyBall.getNumber < 10)
        {
            PlayerPrefs.SetInt(Const.Star, 0);
        }
        else if (destroyBall.getNumber >= 10 && destroyBall.getNumber < 20)
        {
            PlayerPrefs.SetInt(Const.Star, 1);
        }
        else if (destroyBall.getNumber >= 20 && destroyBall.getNumber < 30)
        {
            PlayerPrefs.SetInt(Const.Star, 2);
        }
        else if (destroyBall.getNumber >= 30)
        {
            PlayerPrefs.SetInt(Const.Star, 3);
        }
        int totalStar = PlayerPrefs.GetInt(Const.TotalStar);
        int star = PlayerPrefs.GetInt(Const.Star);
        PlayerPrefs.SetInt(Const.TotalStar, totalStar + star);
    }
}
